package cn.vic.genertor.view;

import cn.org.rapid_framework.generator.GeneratorFacade;
import cn.org.rapid_framework.generator.provider.db.DataSourceProvider;
import cn.org.rapid_framework.generator.util.SystemHelper;
import cn.vic.model.RepidModel;

import java.io.File;

/**
 * Created by vic on 2017/8/9.
 */
public class RepidBoot {
    private RepidModel model;

    public RepidBoot(RepidModel model) {
        this.model = model;
    }

    public void generate() {
        try {
            DataSourceProvider.setJdbc_driver(model.getJdbcDriver());
            DataSourceProvider.setJdbc_url(model.getJdbcUrl());
            DataSourceProvider.setJdbc_username(model.getJdbcUsername());
            DataSourceProvider.setJdbc_password(model.getJdbcPassword());

            GeneratorFacade facade = new GeneratorFacade(); //这里有设置输出路径
            facade.getGenerator().setIncludes(null);//匹配模版
            facade.setOutRootDir(model.getSavePath());
            facade.getGenerator().addTemplateRootDir(new File(model.getTemplatePath()));//模版路径

            //配置数据源 等信息
            facade.generateByTable(model.getTableName());
            if (SystemHelper.isWindowsOS) {
                Runtime.getRuntime().exec("cmd.exe /c start " + model.getSavePath().replace('/', '\\'));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
